#include "helper.h"
#include "stdio.h"


void printM(double* A, int m, int n){
	int i,j;
	for (j=0; j<m ; j++){
		for (i = 0; i<n ; i++)
			printf("%lf ", A[i*m + j]);
		printf("\n");
	}
}

void printMt(double* A, int m, int n){
	int i,j;
	for (i = 0; i<n ; i++){
		for (j=0; j<m ; j++)
			printf("%lf ", A[i*m + j]);
		printf("\n");
	}
}

void printV(double* v, int d){
	int j;
	for (j=0; j<d ; j++)
		printf("%lf\n", v[j]);
}


void fprintM(char* filename,double* A, int m, int n){

	int i,j;
    FILE *fp;
    fp = fopen(filename, "w");
	if(fp == NULL){
		printf("Not able to print to file, %s.",filename);
		return;
	}
   

	for (j=0; j<m ; j++){
		for (i = 0; i<n ; i++)
			if(i != (n-1))
				fprintf(fp,"%lf ", A[i*m + j]);
			else
				fprintf(fp,"%lf", A[i*m + j]);
		fprintf(fp,"\n");
	}
	fclose(fp);
}

void fprintMt(char* filename,double* A, int m, int n){
	int i,j;
    FILE *fp;
    fp = fopen(filename, "w");
	if(fp == NULL){
		printf("Not able to print to file, %s.",filename);
		return;
	}

	for (i = 0; i<n ; i++){
		for (j=0; j<m ; j++)
			if(j != (m-1))
				fprintf(fp,"%lf ", A[i*m + j]);
			else
				fprintf(fp,"%lf", A[i*m + j]);
		fprintf(fp,"\n");
	}
	fclose(fp);
}

void fprintV(char* filename,double* v, int d){
	int j;
    FILE *fp;
    fp = fopen(filename, "w");
	if(fp == NULL){
		printf("Not able to print to file, %s.",filename);
		return;
	}

	for (j=0; j<d ; j++)
		fprintf(fp,"%lf\n", v[j]);
	fprintf(fp,"\n");
	fclose(fp);
}


void fscanM(char* filename,double* A, int m, int n){

	int i,j;
    FILE *fp;
    fp = fopen(filename, "r");
	if(fp == NULL){
		printf("Not able to scan from file, %s.",filename);
		return;
	}
   

	for (j=0; j<m ; j++){
		for (i = 0; i<n ; i++)
			fscanf(fp,"%lf", &A[i*m + j]);
	}
	fclose(fp);
}

void fscanMt(char* filename,double* A, int m, int n){
	int i,j;
    FILE *fp;
    fp = fopen(filename, "r");
	if(fp == NULL){
		printf("Not able to scan from file, %s.",filename);
		return;
	}

	for (i = 0; i<n ; i++){
		for (j=0; j<m ; j++)
			fscanf(fp,"%lf", &A[i*m + j]);
	}
	fclose(fp);
}

void fscanV(char* filename,double* v, int d){
	int j;
    FILE *fp;
    fp = fopen(filename, "r");
	if(fp == NULL){
		printf("Not able to scan from file, %s.",filename);
		return;
	}

	for (j=0; j<d ; j++)
		fscanf(fp,"%lf", &v[j]);
	fclose(fp);
}



